(function(window) {

var effect   = {};
var $effect  = null;
var $loading = null;
var efunc    = function() {};

effect.onCreate = function() {
	$effect  = pcc2.$c.children('.effect');
	$loading = pcc2.$space.children('.loading');
};

/*------- PAGE TRANSITION EFFECT -------*/
var pt = {};
pt.array = [["pt-page-moveToLeft","pt-page-moveFromRight"],["pt-page-moveToRight","pt-page-moveFromLeft"],["pt-page-moveToTop","pt-page-moveFromBottom"],["pt-page-moveToBottom","pt-page-moveFromTop"],["pt-page-fade","pt-page-moveFromRight pt-page-ontop"],["pt-page-fade","pt-page-moveFromLeft pt-page-ontop"],["pt-page-fade","pt-page-moveFromBottom pt-page-ontop"],["pt-page-fade","pt-page-moveFromTop pt-page-ontop"],["pt-page-moveToLeftFade","pt-page-moveFromRightFade"],["pt-page-moveToRightFade","pt-page-moveFromLeftFade"],["pt-page-moveToTopFade","pt-page-moveFromBottomFade"],["pt-page-moveToBottomFade","pt-page-moveFromTopFade"],["pt-page-moveToLeftEasing pt-page-ontop","pt-page-moveFromRight"],["pt-page-moveToRightEasing pt-page-ontop","pt-page-moveFromLeft"],["pt-page-moveToTopEasing pt-page-ontop","pt-page-moveFromBottom"],["pt-page-moveToBottomEasing pt-page-ontop","pt-page-moveFromTop"],["pt-page-scaleDown","pt-page-moveFromRight pt-page-ontop"],["pt-page-scaleDown","pt-page-moveFromLeft pt-page-ontop"],["pt-page-scaleDown","pt-page-moveFromBottom pt-page-ontop"],["pt-page-scaleDown","pt-page-moveFromTop pt-page-ontop"],["pt-page-scaleDown","pt-page-scaleUpDown pt-page-delay300"],["pt-page-scaleDownUp","pt-page-scaleUp pt-page-delay300"],["pt-page-moveToLeft pt-page-ontop","pt-page-scaleUp"],["pt-page-moveToRight pt-page-ontop","pt-page-scaleUp"],["pt-page-moveToTop pt-page-ontop","pt-page-scaleUp"],["pt-page-moveToBottom pt-page-ontop","pt-page-scaleUp"],["pt-page-scaleDownCenter","pt-page-scaleUpCenter pt-page-delay400"],["pt-page-rotateRightSideFirst","pt-page-moveFromRight pt-page-delay200 pt-page-ontop"],["pt-page-rotateLeftSideFirst","pt-page-moveFromLeft pt-page-delay200 pt-page-ontop"],["pt-page-rotateTopSideFirst","pt-page-moveFromTop pt-page-delay200 pt-page-ontop"],["pt-page-rotateBottomSideFirst","pt-page-moveFromBottom pt-page-delay200 pt-page-ontop"],["pt-page-flipOutRight","pt-page-flipInLeft pt-page-delay500"],["pt-page-flipOutLeft","pt-page-flipInRight pt-page-delay500"],["pt-page-flipOutTop","pt-page-flipInBottom pt-page-delay500"],["pt-page-flipOutBottom","pt-page-flipInTop pt-page-delay500"],["pt-page-rotateFall pt-page-ontop","pt-page-scaleUp"],["pt-page-rotateOutNewspaper","pt-page-rotateInNewspaper pt-page-delay500"],["pt-page-rotatePushLeft","pt-page-moveFromRight"],["pt-page-rotatePushRight","pt-page-moveFromLeft"],["pt-page-rotatePushTop","pt-page-moveFromBottom"],["pt-page-rotatePushBottom","pt-page-moveFromTop"],["pt-page-rotatePushLeft","pt-page-rotatePullRight pt-page-delay180"],["pt-page-rotatePushRight","pt-page-rotatePullLeft pt-page-delay180"],["pt-page-rotatePushTop","pt-page-rotatePullBottom pt-page-delay180"],["pt-page-rotatePushBottom","pt-page-rotatePullTop pt-page-delay180"],["pt-page-rotateFoldLeft","pt-page-moveFromRightFade"],["pt-page-rotateFoldRight","pt-page-moveFromLeftFade"],["pt-page-rotateFoldTop","pt-page-moveFromBottomFade"],["pt-page-rotateFoldBottom","pt-page-moveFromTopFade"],["pt-page-moveToRightFade","pt-page-rotateUnfoldLeft"],["pt-page-moveToLeftFade","pt-page-rotateUnfoldRight"],["pt-page-moveToBottomFade","pt-page-rotateUnfoldTop"],["pt-page-moveToTopFade","pt-page-rotateUnfoldBottom"],["pt-page-rotateRoomLeftOut pt-page-ontop","pt-page-rotateRoomLeftIn"],["pt-page-rotateRoomRightOut pt-page-ontop","pt-page-rotateRoomRightIn"],["pt-page-rotateRoomTopOut pt-page-ontop","pt-page-rotateRoomTopIn"],["pt-page-rotateRoomBottomOut pt-page-ontop","pt-page-rotateRoomBottomIn"],["pt-page-rotateCubeLeftOut pt-page-ontop","pt-page-rotateCubeLeftIn"],["pt-page-rotateCubeRightOut pt-page-ontop","pt-page-rotateCubeRightIn"],["pt-page-rotateCubeTopOut pt-page-ontop","pt-page-rotateCubeTopIn"],["pt-page-rotateCubeBottomOut pt-page-ontop","pt-page-rotateCubeBottomIn"],["pt-page-rotateCarouselLeftOut pt-page-ontop","pt-page-rotateCarouselLeftIn"],["pt-page-rotateCarouselRightOut pt-page-ontop","pt-page-rotateCarouselRightIn"],["pt-page-rotateCarouselTopOut pt-page-ontop","pt-page-rotateCarouselTopIn"],["pt-page-rotateCarouselBottomOut pt-page-ontop","pt-page-rotateCarouselBottomIn"],["pt-page-rotateSidesOut","pt-page-rotateSidesIn pt-page-delay200"],["pt-page-rotateSlideOut","pt-page-rotateSlideIn"]];

effect.pt = function(aid, $old, $now, cb) {
	var animate = pt.array[aid || 0];
	$old && $old.addClass(animate[0]);
	$now && $now.addClass(animate[1]);
	$effect.show();
	$now.find('.page-show .item').removeClass('show-effect');

	$now.on('webkitAnimationEnd', function() {
		$now.off('webkitAnimationEnd');
		$old && $old.removeClass(animate[0]);
		$now && $now.removeClass(animate[1]);
		$effect.hide();
		
		$now.find('.page-show .item').addClass('show-effect');

		cb && cb();
	});
};

/*------- PAGE TRANSITION EFFECT -------*/


/*------- LOADING EFFECT -------*/
var _loading = false;
var _loadCB  = null;

var onLoading = function(rate) {
	if(_loading) {
		if(rate >= 1) onLoadEnd();

		rate = Math.round(rate * 100) + ' %';
		$loading.find('.process').text(rate);
	}
};
var onLoadEnd = function() {
	_loading = false;
	window.setTimeout(function() {
		$loading.find('.process').hide();
		$loading.find('.logo').transit({
			'margin-top': 23,
			'opacity':    0.5
		}, 1000, function() {
			_loadCB && _loadCB();
			pcc2.$space.fadeOut($loading.remove);
		});
	}, 700);
};
/**
 * loading effect
 * @param  {Function} cb 回调函数
 * @return {Function}    更新rate函数
 */
effect.loading = function(cb) {
	if(config.ondebug('noPreload')) {
		_loadCB  = cb;
		_loading = false;
		_loadCB && _loadCB();
		pcc2.$space.fadeOut($loading.remove);
		return function() {};
	}
	if(!_loading) {
		_loading = true;
		_loadCB  = cb;
		return onLoading;
	}
};
/*------- LOADING EFFECT -------*/

window.effect = effect;

})(window);